﻿



-- =============================================
-- Author:		Dorin Popa
-- Create date: 21.05.2011
-- Description:	Genereaza analitice pentru conturile care au articole si alineate
-- =============================================
CREATE PROCEDURE [dbo].[balcon_GenerareAnaliticeArticoleAlineate] 
	-- Add the parameters for the stored procedure here
	@Cont TipCont, 
	@CodUnitate smallint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	INSERT INTO [Conturi]
           ([Cont]
           ,[CodUnitate]
           ,[Denumire]
           ,[Tip]
           ,[Terminal]
           ,[ContParinte]
           ,[EsteAnalitic]
           ,[DebitInitial]
           ,[CreditInitial]
           ,[DebitPrecedent]
           ,[CreditPrecedent]
           ,[Categorie]
           ,[RulajDebit]
           ,[RulajCredit]
           ,[Sold])
           
     SELECT RTRIM(LTRIM(@Cont)) +'.' + RTRIM(LTRIM([ArtAl])) AS [Cont]
      ,aa.[CodUnitate]
      ,aa.[Denumire]
      ,cc.Tip
      ,aa.[Terminal]
      ,RTRIM(LTRIM(@Cont)) + CASE WHEN RTRIM(LTRIM([AAParinte]))='N/A' THEN '' ELSE '.' +RTRIM(LTRIM([AAParinte])) END  AS [ContParinte]
      ,1 AS [EsteAnalitic]
      ,0,0,0,0
      ,cc.[Categorie]
      ,0,0,0
	 FROM [ArticoleAliniate] aa JOIN [Conturi] cc ON aa.CodUnitate=cc.CodUnitate AND cc.Cont=@cont AND cc.CodUnitate=@CodUnitate
	 WHERE aa.CodUnitate = @CodUnitate AND [Categorii] like '%[CLT]%'  AND [ArtAl]<>'N/A'
	 AND NOT EXISTS(SELECT * FROM [Conturi] WHERE [CodUnitate]=@CodUnitate AND 
	 [CONT]=RTRIM(LTRIM(@Cont)) +'.' + RTRIM(LTRIM(aa.[ArtAl])))
	 
	 
END